package com.lcy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcy.entity.Book;
import com.lcy.mapper.BookMapper;
import com.lcy.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.vo.BarVO;
import com.lcy.vo.PieVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-03-27
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired(required = false)
    private BookMapper mapper;

    @Override
    public List<Book> findByPage(Integer page, Integer limit) {
        //分页查询
        IPage<Book> pages = new Page<>(page,limit);
        IPage<Book> books = mapper.selectPage(pages,null);
        return books.getRecords();
    }

    @Override
    public List<PieVO> pie() {
        List<PieVO> pieVOList = new ArrayList<>();
        //先查出数据
        List<Book> bookList = this.mapper.selectList(null);
        for (Book book : bookList) {
            PieVO pieVO = new PieVO();
            pieVO.setName(book.getName());
            pieVO.setValue(book.getPrice());
            pieVOList.add(pieVO);
        }
        return pieVOList;
    }

    @Override
    public BarVO bar() {
        BarVO barVO = new BarVO();
        List<String> names = new ArrayList<>();
        List<Float> values = new ArrayList<>();
        //先查出数据
        List<Book> bookList = this.mapper.selectList(null);
        for (Book book : bookList) {
          names.add(book.getName());
          values.add(book.getPrice());
        }
        barVO.setNames(names);
        barVO.setValues(values);
        return barVO;
    }


}
